
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
    <div>
        <input id="name" />
    </div>
    <div>
        <input id="pwd" class="hh"/>
    </div>
    <div>
        <input type="radio" value="1" name="role" checked="checked"/>学生
        <input type="radio" value="2" name="role"/>老师
    </div>
    <div>
        <button id="login" value="">登录</button>
        <button id="register" value="">注册</button>
    </div><br/><br/>
    <form action="/article/upload" method = 'post' enctype="multipart/form-data">
        <div>
            选择文件<input type="file" id="file"/>
        </div>
        <div>
            <button id="upload" type="submit" value="">上传</button>
        </div>
    </form>


    <script type="application/javascript" src="../../js/jquery-3.1.1.min.js"></script>
    <script>
        $("#upload").click(function(){

        });
        $(function () {

        });

        $("#login").click(function () {
            var username=$("#name").val();
            var pwd=$("#pwd").val();
            //alert(username+pwd);
            //console.log(username);
            var url=getRootPath()+"/user/login";
            var data={
                username:username,
                pwd:pwd
            };
            $.ajax({
                url:url,
                data:JSON.stringify(data),
                type:"put",
                contentType : "application/json;charset=utf-8",
                success:function (data) {
                    if (data.res==1){
                        alert("登录成功");
                    }else {
                        alert(data.msg);
                    }
                },
                error:function (res,data) {
                    alert("链接失效了！");
                    console.log(res);
                    console.log(data);

                }

            });
        });
        function getRootPath()
        {
            var pathName = window.location.pathname.substring(1);
            var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
            return window.location.protocol + '//' + window.location.host + '/'+ webName ;
        }

        $("#register").click(function () {
            var username=$("#name").val();
            var pwd=$("#pwd").val();
            var  role=$("input[type='radio']:checked").val();
            var url=getRootPath()+"register";
            var data={
                username:username,
                pwd:pwd,
                role:role
            };

            $.ajax({
                url:url,
                data:JSON.stringify(data),
                type:"put",
                contentType : "application/json;charset=utf-8",
                success:function (data) {
                    if (data.res==1){
                        alert("注册成功");
                    }else {
                        alert(data.msg);
                    }
                },
                error:function (res,data) {
                    alert("链接失效了！");
                    console.log(res);
                    console.log(data);

                }
            })

        });
    </script>
</body>
</html>
